package dbexp.model;

import java.util.Vector;

import dbexp.model.db.DbOT;

public class User extends DbObject {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public static final String LABEL = "Użytkownicy";
	public static final String DEF_ICON = "user-icon";
	protected Vector<String> role = new Vector<String>();
	protected Vector<String> systemPrivs = new Vector<String>();

	public User() {
		super(LABEL, DbOT.USERS);
		setIcons("users-icon");
	}

	public User(String userName) {
		super(userName, DbOT.USER);
		setIcons(DEF_ICON, "users-icon");
		set("leaf", true);
	}

	public String getProfile() {
		return get("PROFILE", "DEFAULT");
	}

	public void setProfile(String profile) {
		set("PROFILE", profile);
	}

	public String getPassword() {
		return get("PASSWORD");
	}

	public void setPassword(String password) {
		set("PASSWORD", password);
	}

	public String getLockDate() {
		return get("LOCKDATE");
	}

	public void setLockDate(String lockDate) {
		set("LOCKDATE", lockDate);
	}

	public String getExpiryDate() {
		return get("EXPIRYDATE");
	}

	public void setExpiryDate(String expiryDate) {
		set("EXPIRYDATE", expiryDate);
	}

	public boolean isExpired() {
		return is("EXPIRED", false);
	}

	public void setExpired(boolean expired) {
		set("EXPIRED", expired);
	}

	public boolean isOpen() {
		return is("OPEN", true);
	}

	public void setOpen(boolean open) {
		set("OPEN", open);
	}

	public boolean isLocked() {
		return !isOpen();
	}

	public void setLocked(boolean locked) {
		setOpen(!locked);
	}

	public String getStatus() {
		return get("STATUS");
	}

	public void setStatus(String status) {
		set("STATUS", status);
		if (status.indexOf("OPEN") != -1) {
			setOpen(true);
		}
		if (status.indexOf("EXPIRED") != -1) {
			setExpired(true);
		}
		if (status.indexOf("LOCKED") != -1) {
			setLocked(true);
		}
	}

	public String getDefaultTablespace() {
		return get("DEFAULT_TABLESPACE");
	}

	public void setDefaultTableSpace(String tablespace) {
		set("DEFAULT_TABLESPACE", tablespace);
	}

	public String getTemporaryTablespace() {
		return get("TEMPORARY_TABLESPACE");
	}

	public void setTemporaryTableSpace(String tablespace) {
		set("TEMPORARY_TABLESPACE", tablespace);
	}

	public String getCreated() {
		return get("CREATED");
	}

	public void setCreated(String created) {
		set("CREATED", created);
	}

	public Vector<String> getRole() {
		return role;
	}

	public void setRole(Vector<String> role) {
		this.role = role;
	}

	public Vector<String> getSystemPrivs() {
		return systemPrivs;
	}

	public void setSystemPrivs(Vector<String> systemPrivs) {
		this.systemPrivs = systemPrivs;
	}

	public void addRole(String rola) {
		role.add(rola);
	}

	public boolean isRole(String rola) {
		return role.contains(rola);
	}

	public void addSystemPrivs(String sp) {
		systemPrivs.add(sp);
	}

	public boolean isSystemPrivs(String sp) {
		return systemPrivs.contains(sp);
	}

	private String getQuery(String string) {
		String out = string;
		if (requiredFieldIsNotNull()) {
			out += "USER \"" + getName() + "\" \n";
			out += "PROFILE \"" + getProfile() + "\" \n";
			if (isNotNullString("PASSWORD")) {
				out += "IDENTIFIED BY \"" + getPassword() + "\" ";
			}
			if (isExpired()) {
				out += "PASSWORD EXPIRE \n";
			}
			out += "DEFAULT TABLESPACE \"" + getDefaultTablespace() + "\" \n";
			out += "ACCOUNT " + ((isOpen()) ? "UNLOCK" : "LOCK") + ";\n";
			for (int i = 0; i < systemPrivs.size(); i++) {
				out += "GRANT " + systemPrivs.get(i) + " TO \"" + getName()
						+ "\";\n";
			}
			for (int i = 0; i < role.size(); i++) {
				out += "GRANT " + role.get(i) + " TO \"" + getName() + "\";\n";
			}
		}
		return out;
	}

	public String getQuery(int operation) {
		if (operation == CREATE) {
			return getQuery("CREATE ");
		} else if (operation == ALTER) {
			return getQuery("ALTER ");
		} else if (operation == DELETE) {
			return "DROP USER " + getName() + "";
		}
		return oneFromDual();
	}

}
